<?php
/**
 * Created by PhpStorm.
 * User: reshared
 * Date: 17-7-21
 * Time: 下午11:18
 */

namespace App\Repository;


use App\Models\ArticleModel;
use App\Models\TagBlindModel;

class ArticleRepository extends BaseRepository
{
    protected $tagBlindModel;

    public function __construct(ArticleModel $articleModel, TagBlindModel $blindModel)
    {
        $this->model = $articleModel;

        $this->tagBlindModel = $blindModel;
    }

    public function getIndexArticles($number = 15)
    {
        return $this->model->orderBy('views', 'desc')->limit($number)->get();
    }

    public function getList($tid, $number = 15)
    {
        $relations = $this->tagBlindModel->where('tid', $tid)->get();
        $aids = [];
        foreach ($relations as $relation) {
            $aids[] = $relation->aid;
        }
        return $this->model->whereIn('aid', $aids)->orderBy('updated_at', 'desc')->paginate($number);
    }

    public function getByTid($tid, $pagesize = 15)
    {
        return $this->model->whereIn('tid',  $this->getAidsByTid($tid))->paginate($pagesize);
    }

    public function getArticles($pagesize = 15)
    {
        return $this->model->paginate($pagesize);
    }

    public function getAidsByTid($tid)
    {
        $ids = [];
        $results = $this->tagBlindModel->where('tid', $tid)->get('aid');
        foreach ($results as $result) {
            $ids[] = $result->aid;
        }
        return $ids;
    }
}